package com.cash.channel.svc.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.callback.BankCardCallBack;
import com.cash.common.model.Result;
import com.cash.user.model.UserChannelCardPackModel;
import com.cash.user.service.UserChannelService;
import com.google.common.collect.Maps;
import geex.trans.req.UpdateLoanMainCardReq;
import geex.trans.rsp.BaseRsp;
import geex.trans.service.TransCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
public class BankCardLogic {

    @Autowired
    private UserChannelService userChannelService;

    @Autowired
    private OrderLogic orderLogic;

    @Autowired(required = false)
    private TransCardService transCardService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public Boolean checkCardExist(String idNo, String name, String bankCardNum, String phone, String bankCode) {
        UserChannelCardPackModel checkCard = new UserChannelCardPackModel();
        checkCard.setcIdno(idNo);
        checkCard.setcName(name);
        checkCard.setcCardNo(bankCardNum);
        checkCard.setcReserveMobile(phone);
        checkCard.setcBankCode(bankCode);
        Boolean cardRes = userChannelService.checkUserExistCard(checkCard);
        return cardRes;
    }

    public Boolean setChannelDefaultCard(String cardNo, String uid, String channel) throws CashChannelException {
        Result setResult = userChannelService.setChannelDefaultCard(cardNo, uid, channel);
        if (1 == setResult.getCode()) {
            throw new CashChannelException(setResult.getMsg());
        }
        return true;
    }

    public void updateFundCard(String wdId, String name, String idNo, String cardNo, String bankCode, String mobile) throws CashChannelException {
        //增加通知资金平台换卡
        try {
            if (!StringUtils.isEmpty(wdId)) {
                log.info("updateFundCard 查询提现订单状态:" + wdId);
                Map<String, Object> wdOrderInfoModel = orderLogic.findWithdrawAppInfoByAppId(wdId);
                log.info("订单信息:" + JSON.toJSONString(wdOrderInfoModel));
                if (wdOrderInfoModel != null && MapUtils.getInteger(wdOrderInfoModel, "nWdAppStatus", 0) == 1500) {
                    //通知资金换卡
                    updateLoanMainCard(name, idNo, wdId, cardNo, bankCode, mobile);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CashChannelException("通知资金平台换卡失败");
        }

    }

    /**
     * 资金换卡
     *
     * @param name
     * @param idNO
     * @param appId
     * @param bankCard
     * @param bankCode
     * @param mobile
     * @return
     * @throws Exception
     */
    private Boolean updateLoanMainCard(String name, String idNO, String appId, String bankCard, String bankCode, String mobile) throws Exception {
        //更换提现订单银行卡
        UpdateLoanMainCardReq updateLoanMainCardReq = new UpdateLoanMainCardReq();
        updateLoanMainCardReq.setName(name);
        updateLoanMainCardReq.setIdNo(idNO);
        updateLoanMainCardReq.setAppId(appId);
        updateLoanMainCardReq.setBankCard(bankCard);
        updateLoanMainCardReq.setBankCode(bankCode);
        updateLoanMainCardReq.setMobile(mobile);
        BaseRsp baseRsp = transCardService.updateLoanMainCard(updateLoanMainCardReq);
        if (baseRsp.isSuccess()) {
            return true;
        }
        log.info("updateLoanMainCard  换卡失败:" + baseRsp.getErrorMessage());
        throw new Exception("换卡失败");
    }

    public void addChannelBankCardToPack(String uid, String channel, String bankCode, String bankName, String cardNo, String idNo, String name, String reserveMobile) throws CashChannelException {
        UserChannelCardPackModel userChannelCardPackModel = new UserChannelCardPackModel();
        userChannelCardPackModel.setcUid(uid);
        userChannelCardPackModel.setcChannel(channel);
        userChannelCardPackModel.setnDefault(true);
        //将渠道银行转为我方银行code、名称
        userChannelCardPackModel.setcBankCode(bankCode);
        userChannelCardPackModel.setcBankName(bankName);
        userChannelCardPackModel.setcCardNo(cardNo);
        userChannelCardPackModel.setcIdno(idNo);
        userChannelCardPackModel.setcName(name);
        userChannelCardPackModel.setCreateTime(new Date());
        userChannelCardPackModel.setcReserveMobile(reserveMobile);
        userChannelCardPackModel.setnDel(false);
        Result cardResult = this.userChannelService.saveUserChannelCardPack(userChannelCardPackModel);
        if (1 == cardResult.getCode()) {
            throw new CashChannelException(cardResult.getMsg());
        }
    }

    public void feedBackBindCard(String orderNo, Integer status, String bankCode, String bankName, String bankCard, String mobile, String remark, String channel, Boolean isCustom) {
        JSONObject noticeMap = new JSONObject();
        noticeMap.put("channel", channel);
        noticeMap.put("orderId", orderNo);
        noticeMap.put("isCustom", isCustom);
        noticeMap.put("bankCardInfo", new BankCardCallBack(orderNo, status, bankCode, bankName, bankCard, mobile, remark));
        rabbitTemplate.convertAndSend(Constant.ProConstant.CHANNEL_BANK_CARD_BIND_CALLBACK, noticeMap.toJSONString());
    }

}
